package libs;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Utils {

	public static List<Class<?>> getAllClasses(String pckgname) {
		try {
			List<Class<?>> classes = new ArrayList<Class<?>>();
			// Get a File object for the package
			File directory = null;
			try {
				directory = new File(Thread.currentThread().getContextClassLoader()
						.getResource(pckgname.replace('.', '/')).getFile());
			} catch (NullPointerException x) {
				System.out.println("Nullpointer");
				throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
			}
			if (directory.exists()) {
				// Get the list of the files contained in the package
				String[] files = directory.list();
				for (int i = 0; i < files.length; i++) {
					// we are only interested in .class files
					if (files[i].endsWith(".class")) {
						// removes the .class extension
						classes.add(Class.forName(pckgname + '.' + files[i].substring(0, files[i].length() - 6)));
					}
				}
			} else {
				System.out.println("Directory does not exist");
				throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
			}
			return classes;

		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Class<?>>();
		}
	}
}
